// Flag1Control.cpp
#include "Flag1Control.h"
#include <QDebug>
/**********************************************
* @file       flag1control.cpp
* @模板
* {
  "lock": {
    "flag1": {
      "label1": "lock",
      "pushbutton1": {
        "name": "lock",
        "Commands": "lock(s=<value>)"
      },
      "pushbutton2": {
        "name": "unlock",
        "Commands": "lock(s=<value>)"
      }
    }
  }
}
* @author     yfk
* @date       2025-05-27
**********************************************/

Flag1Control::Flag1Control(const QJsonObject &flagDetails, const QString deviceID,QWidget *parent)
    : QWidget(parent)
{
    setupControlLayout(flagDetails);  // 设置控件的布局
    mDeviceName = deviceID;
}

void Flag1Control::setupControlLayout(const QJsonObject &flagDetails)
{
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    // ========== 外层容器 ==========
    QWidget *outerWidget = new QWidget(this);
    QHBoxLayout *outerLayout = new QHBoxLayout(outerWidget);
    outerLayout->setContentsMargins(0, 0, 0, 0);

    // ========== 内层控件区域 ==========
    QWidget *contentWidget = new QWidget(outerWidget);
    contentWidget->setStyleSheet(R"(
        QWidget {
            background-color: #22314a;
            border-radius: 6px;
            padding: 6px 10px;
        }
    )");
    QHBoxLayout *contentLayout = new QHBoxLayout(contentWidget);
    contentLayout->setContentsMargins(6, 2, 6, 2);
    contentLayout->setSpacing(16);
    contentLayout->setAlignment(Qt::AlignLeft);  // ←←← 保证所有控件靠左对齐

    // label
    QLabel *label = new QLabel(flagDetails["label1"].toString(), contentWidget);
    label->setStyleSheet(R"(
        QLabel {
            font-size: 18px;
            color: white;
            font-weight: bold;
        }
    )");
    label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
    contentLayout->addWidget(label);

    // 读取按钮内容
    QJsonObject pushButton1 = flagDetails["pushbutton1"].toObject();
    QJsonObject pushButton2 = flagDetails["pushbutton2"].toObject();

    QString button1Text = pushButton1["name"].toString();
    QString button2Text = pushButton2["name"].toString();

    // 命令存入哈希表
    buttonCommandMap.insert(button1Text, pushButton1["Commands"].toString());
    buttonCommandMap.insert(button2Text, pushButton2["Commands"].toString());

    // ========== 按钮组 ==========
    QPushButton *button1 = new QPushButton(QIcon(":/img/start.png"), button1Text, contentWidget);
    button1->setObjectName("btn_" + button1Text);
    button1->setCursor(Qt::PointingHandCursor);
    button1->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #2196F3, stop:1 #0D47A1);
            color: white;
            font-size: 18px;
            font-weight: bold;
            padding: 4px 10px;
            border: none;
            border-radius: 6px;
            min-width: 100px;
            max-width: 200px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #0D47A1, stop:1 #2196F3);
        }
    )");
    button1->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);  // ←←← 防止扩展
    contentLayout->addWidget(button1);

    QPushButton *button2 = new QPushButton(QIcon(":/img/stop.png"), button2Text, contentWidget);
    button2->setObjectName("btn_" + button2Text);
    button2->setCursor(Qt::PointingHandCursor);
    button2->setStyleSheet(R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #FFC107, stop:1 #FF6F00);
            color: #333;
            font-size: 18px;
            font-weight: bold;
            padding: 4px 10px;
            border: none;
            border-radius: 6px;
            min-width: 100px;
            max-width: 200px;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                                        stop:0 #FF6F00, stop:1 #FFC107);
        }
    )");
    button2->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);  // ←←← 防止扩展
    contentLayout->addWidget(button2);

    // 添加控件区域到外层
    outerLayout->addWidget(contentWidget);
    mainLayout->addWidget(outerWidget);

    // 信号连接
    connect(button1, &QPushButton::clicked, this, &Flag1Control::onButtonClicked);
    connect(button2, &QPushButton::clicked, this, &Flag1Control::onButtonClicked);
}

void Flag1Control::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (button) {
        QString buttonText = button->text();
        qDebug() << "Button clicked: " << buttonText;

        // 查找并执行对应的命令
        if (buttonCommandMap.contains(buttonText)) {
            QString command = buttonCommandMap.value(buttonText);
            qDebug() << "Executing command: " << command;
            // 在这里可以执行命令，或者调用其他函数来处理命令

            pubMsg(command);
        } else {
            qWarning() << "No command found for button: " << buttonText;
        }
    }
}


void Flag1Control::attachToNode(QSharedPointer<DeviceNode> node)
{
    m_node = node;
}

void Flag1Control::pubMsg(QString content)
{
    if(m_node)
    {
        QString id = mDeviceName;
        //QString id = "ORS_Cask";
        m_node->publishCmd(id,content);
    }
}
